package com.getpebble.android.common.util;

import android.bluetooth.BluetoothClass;
import android.content.Context;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Trace;

public class DiscoveryUtils {
    private static final String[] KNOWN_PEBBLE_ADDRESS_PREFIXES = new String[]{"D0:07:90", "00:18:33", "00:18:34", "D8:95:2F"};
    private static final String TAG = DiscoveryUtils.class.getSimpleName();

    public static boolean isPebbleDevice(String address, BluetoothClass btClass) {
        if (btClass == null) {
            Trace.debug(TAG, address + " => got a NULL bt class");
        } else if (!BuildDependentDiscoveryUtil.isApplicableDebugPebbleDevice(address)) {
            return false;
        } else {
            if (btClass.getDeviceClass() == 1796) {
                Trace.debug(TAG, address + " => is a Pebble");
                return true;
            } else if (btClass.getDeviceClass() == 7936) {
                Trace.debug(TAG, address + " => is uncategorized.  falling back to prefix search.");
                if (address != null) {
                    for (String prefix : KNOWN_PEBBLE_ADDRESS_PREFIXES) {
                        if (address.toLowerCase().startsWith(prefix.toLowerCase())) {
                            Trace.debug(TAG, address + " => prefix search positive");
                            return true;
                        }
                    }
                }
            } else {
                Trace.debug(TAG, address + " => is NOT a Pebble (class = " + btClass.toString() + ")");
            }
        }
        Trace.debug(TAG, address + " => not a Pebble");
        return false;
    }

    public static String getPebbleDisplayName(String name, String address, Context context) {
        if (name != null && !name.isEmpty()) {
            return name;
        }
        if (address == null) {
            return null;
        }
        if (address.isEmpty()) {
            return null;
        }
        String[] parts = address.split(":");
        if (parts.length < 2) {
            return null;
        }
        return context.getString(R.string.pebble) + " " + parts[parts.length - 2] + parts[parts.length - 1];
    }
}
